/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.devs.benchmark.topology;

import java.util.List;
import org.devs.benchmark.Util;
import org.devs.benchmark.modeling.ConnectionType;
import org.devs.benchmark.modeling.CoupledType;
import org.devs.benchmark.modeling.ModelType;

/**
 *
 * @author Alejandro Moreno
 */
public class ClosedTridMesh extends TridMesh {

    public ClosedTridMesh(String name, int width, int height, int depth, Class message) throws Exception {

        super(name, width, height, depth, message);

    }

    public ClosedTridMesh(String name, int width, int height, int depth, Class message, int recursion) throws Exception {

        super(name, width, height, depth, message, recursion);

    }

    @Override
    public void connectSructs(CoupledType coupled, int width, int height, int depth, List<List<ModelType>> structs) {


        for (int j = 0; j < (width * height); j++) {

            for (int i = 0; i < structs.size(); i++) {
                int q = (i + 1) % structs.size();

                List<ModelType> struct1 = structs.get(i);
                List<ModelType> struct2 = structs.get(q);

                coupled.getConnections().add(new ConnectionType(struct1.get(j), struct1.get(j).getOutports().get(4), struct2.get(j), struct2.get(j).getInports().get(5)));
                coupled.getConnections().add(new ConnectionType(struct2.get(j), struct2.get(j).getOutports().get(5), struct1.get(j), struct1.get(j).getInports().get(4)));


            }
        }

    }

    @Override
    public void connectNodes(CoupledType coupled, int width, int height, int depth) {

        if (coupled.getModels().size() > 0) {

            for (int i = 0; i < 6; i++) {

                addConnectionToParent(coupled, 0, i, i);
                addConnectionFromParent(coupled, 0, i, i);

            }

            List<List<ModelType>> structs = Util.partition(coupled.getModels(), width * height);

            connectSructs(coupled, width, height, depth, structs);

            for (int q = 0; q < depth; q++) {

                List<ModelType> models = structs.get(q);

                for (int i = 0; i < models.size(); i++) {

                    int j = (i + 1);
                    if (j % width == 0) {
                        j = (j - width);
                    }

                    addConnection(coupled, i, 1, j, 3);
                    addConnection(coupled, j, 3, i, 1);

                    int k = ((i + width) % models.size());

                    addConnection(coupled, i, 2, k, 0);
                    addConnection(coupled, k, 0, i, 2);
                }
            }

        }


    }
}
